package com.woniu.dao;

import com.woniu.dto.*;
import com.woniu.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import com.woniu.pojo.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import sun.awt.SunHints;

import java.util.List;

@MapperScan
public interface GoodsMapper {

    int deleteByPrimaryKey(Long id);

    int insert(Goods record);

    int insertSelective(Goods record);

    Goods selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Goods record);

    int updateByPrimaryKey(Goods record);

    //查询展示的页面和所有信息
    @Select("select * from vendor")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column = "id", property = "goods", many = @Many(select = "findGoodsByVid", fetchType = FetchType.EAGER))
    })
    public List<Vendor> findAll();

    @Select("select * from goods where vendor_id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column = "id", property = "gds", many = @Many(select = "findGoodDetailById", fetchType = FetchType.EAGER))
    })
    List<Goods>findGoodsByVid(long id);

    @Select("select * from goods_detail where g_id = #{id}")
    List<GoodsDetail> findGoodDetailById(Long id);


    //查询一共有多少评论数量
    @Select("SELECT COUNT(goods_id) FROM goods_comments where goods_id=#{id}")
    int findConNum(int id);

    //发布商品
    @Insert({"INSERT INTO goods VALUES(#{goods.id},#{goods.vendorId},#{goods.title}," +
            "#{goods.description},#{goods.price},#{goods.createTime}," +
            "#{goods.stock},#{goods.status},#{goods.goodsType},#{goods.likeNum},#{goods.updateTime})"})
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    void addshop(@Param("goods") Addgoods goods);

    //添加图片
    @Insert({"insert into goods_detail(g_id,goods_img,create_time,update_time) values(#{goods.id}," +
            "#{str},#{goods.createTime},#{goods.updateTime})"})
    void  addphoto(@Param("str") String str,@Param("goods") Addgoods goods);


    //查询单个信息
    @Select("select * from goods " +
            "left join  goods_detail " +
            "on goods.id = goods_detail.g_id " +
            "left join vendor " +
            "on goods.vendor_id = vendor.id where goods.id=#{id}")
    FindGoods findOne(int id);



    //查询所有的父级评论
        @Select("select a.id,a.goods_id,a.content,a.create_time,a.tags,a.parent_id,b.title,b.admin_card from goods_comments as a " +
                "left join admin as b on a.user_id=b.id " +
                "where a.goods_id =#{id}")
        List<RestCom> findCom(int id);


        //查询对应父级评论的子评论
        @Select("select a.id,a.goods_id,a.content,a.create_time,,a.tags,a.parent_id,b.title,b.admin_card from goods_comments as a " +
                "left join admin as b on a.user_id=b.id " +
                "where a.goods_id =#{id} and parent_id=#{parentid}")
        List<RestCom> findzicom(@Param("id") int id,@Param("parentid") long parentid );



        //查询单个普通商品点击量
        @Select("select like_num from goods where id=id")
        int  findlikenum( int id);

        //更新点击量
        @Update("update goods set like_num=#{num} where goods.id=#{id} ")
        void updatalikenum(@Param("id") int id, @Param("num") int likenum);


    //这是查询详细的商品 商户信息
    @Select("select * from goods where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column = "vendor_id", property = "vendor", one = @One(select = "findonevendor", fetchType = FetchType.EAGER))
    })
    Restfind findrestByVid(int id);

    @Select("select * from vendor where id=#{vendor_id}")
    public Vendor findonevendor(int vendor_id);


    //收藏 查询对应的商品是否有对应的人收藏
    @Select("select * from goods_collect where collect_id=#{id} and user_id=#{userid}")
    public GoodsCollect findbygoodsidanduserid(@Param("id") int id,@Param("userid") long userid);


    //如果没有 就新增对应的收藏
    @Insert("insert into goods_collect values(#{goodsCollect.id},#{goodsCollect.collectId},#{goodsCollect.collectTime},#{goodsCollect.userId})")
    void addcollect(@Param("goodsCollect") GoodsCollect goodsCollect);


    //如果有对应的收藏 那么就删除这个收藏
    @Delete("delete from goods_collect where id=#{id}")
    void deletecollectbyid (long id);



    //根据类型查询所有商品
    @Select({"select * from goods where goods_type=#{leixing}"})
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column ="vendor_id", property = "vendorrList", many = @Many(select = "findbyid", fetchType = FetchType.EAGER))
    })
    List<good> findbyleixing(@Param("leixing") String leixing);

    @Select({"select * from vendor where id = #{vendor_id}"})
    /*@Results({
            @Result(id = true, property = "id", column = "id"),
*//*
            @Result(column = "goods_id", property = "goodDtetaills", many = @Many(select = "findbyleixing", fetchType = FetchType.EAGER))
*//*


    })*/
    List<vendorr> findbyid(@Param("vendor_id") long vendor_id);


   /* @Select({"select * from goods_detail where g_id = #{goods_id}"})
    List<goodDtetaill> findbyleixing(long goods_id);*/





   //查询自己订单里面的商品
    @Select("select * from orders where user_id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column ="id", property = "ordersXq", one = @One(select = "findorderxq", fetchType = FetchType.EAGER))
    })
    Orderss findorder(int id);

    @Select("select * from order_detail where o_id=#{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column ="g_id", property = "shop", one = @One(select = "findbyshop", fetchType = FetchType.EAGER))
    })
    OrdersXq findorderxq(long id);

    @Select("select * from goods where id=#{g_id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(column ="id", property = "shopxq", many = @Many(select = "findbyshopxq", fetchType = FetchType.EAGER))
    })
    Shop findbyshop(long g_id);

    @Select("select * from goods_detail where g_id=#{id}")
    ShopXq findbyshopxq(long id);



    //新增订单



}